package com.huahua.graduateschoolspringboot.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.huahua.graduateschoolspringboot.entity.BaseColumn;
import com.huahua.graduateschoolspringboot.entity.R;
import com.huahua.graduateschoolspringboot.entity.po.CityAlarm;
import com.huahua.graduateschoolspringboot.entity.vo.AlarmCountVo;
import com.huahua.graduateschoolspringboot.entity.vo.CityAlarmCountVo;
import com.huahua.graduateschoolspringboot.enums.DeviceTypeEnum;
import com.huahua.graduateschoolspringboot.service.CityAlarmService;
import com.huahua.graduateschoolspringboot.util.MessageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author LoveHuaHua
 * @date 2023年05月08日 17:01
 * @description believe in yourself
 */
@RestController
@RequestMapping("/alarm")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class AlarmController {

    private final CityAlarmService cityAlarmService;

    @PostMapping("/closeAlarm")
    public Object closeAlarm() {
        MessageUtil.sendMessage("{}", DeviceTypeEnum.ALARM, 0);
        CityAlarm cityAlarm = cityAlarmService.lambdaQuery().orderByDesc(BaseColumn::getId).last("limit 1").one();
        if (cityAlarm.getStatus() == 1){
            cityAlarm.setStatus(0);
            cityAlarmService.updateById(cityAlarm);
        }
        return R.success();
    }

    /**
     * 获取报警状态
     *
     * @return
     */
    @GetMapping("/getAlarmStatus")
    public Object getAlarmStatus() {
        CityAlarm cityAlarm = cityAlarmService.lambdaQuery().orderByDesc(BaseColumn::getId).last("limit 1").one();
        return R.success(cityAlarm);
    }



    /**
     * 获取报警计数
     *
     * @return {@link Object}
     */
    @GetMapping("/getAlarmCount")
    public Object getAlarmCount() {
        CityAlarmCountVo cityAlarmVo = new CityAlarmCountVo();
        Date date = new Date();
        Long alarmCountDay = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfDay(date), DateUtil.endOfDay(date)).count();
        Long alarmCountWeek = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfWeek(date), DateUtil.endOfWeek(date)).count();
        Long alarmCountMonth = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfMonth(date), DateUtil.endOfMonth(date)).count();
        Long alarmCountYear = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfYear(date), DateUtil.endOfYear(date)).count();
        cityAlarmVo.setAlarmCountDay(alarmCountDay);
        cityAlarmVo.setAlarmCountWeek(alarmCountWeek);
        cityAlarmVo.setAlarmCountMonth(alarmCountMonth);
        cityAlarmVo.setAlarmCountYear(alarmCountYear);
        return R.success(cityAlarmVo);
    }

    @GetMapping("/getTodayAlarmCount")
    public Object getTodayAlarmCount(){
        AlarmCountVo todayAlarmCountVo = new AlarmCountVo();
        ArrayList<Long> todayAlarmCounts = new ArrayList<>();
        String today = DateUtil.today();
        for (int i = 0; i < 24; i++) {
            Long zeroClock = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, today + " "+StrUtil.fillBefore(String.valueOf(i),'0',2)+":00:00", today + " "+StrUtil.fillBefore(String.valueOf(i),'0',2) + ":59:59").count();
            todayAlarmCounts.add(zeroClock);
        }
        todayAlarmCountVo.setAlarmCountHour(todayAlarmCounts);

        return R.success(todayAlarmCountVo);
    }

    @GetMapping("/getWeekAlarmCount")
    public Object getWeekAlarmCount(CityAlarm cityAlarm){
        AlarmCountVo alarmCountVo = new AlarmCountVo();
        //这周的开始时间
        ArrayList<Long> weekAlarmCounts = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfWeek(new Date());
        for (int i = 0; i < 7; i++) {
            Long currentDay = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfDay(dateTime), DateUtil.endOfDay(dateTime)).count();
            weekAlarmCounts.add(currentDay);
            //每次加一天
            dateTime = dateTime.offset(DateField.DAY_OF_MONTH,1);
        }
        alarmCountVo.setAlarmCountWeek(weekAlarmCounts);
        return R.success(alarmCountVo);
    }
    @GetMapping("/getMonthAlarmCount")
    public Object getMonthAlarmCount() {
        AlarmCountVo alarmCountVo = new AlarmCountVo();
        //月开始的时间
        DateTime ofYear = DateUtil.beginOfYear(new Date());
        ArrayList<Long> monthAlarmCounts = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Long currentMonth = cityAlarmService.lambdaQuery().between(CityAlarm::getCreateTime, DateUtil.beginOfMonth(ofYear), DateUtil.endOfMonth(ofYear)).count();
            monthAlarmCounts.add(currentMonth);
            //每次加1个月
            ofYear.offset(DateField.MONTH,1);
        }
        alarmCountVo.setAlarmCountMonth(monthAlarmCounts);
        return R.success(alarmCountVo);
    }

}
